<script>
	$(document).ready(function() {
		$( "#condEnd" ).hide();
		$( "#eStrtId" ).datepicker();
		
		
		function clickHandler() {
			var chkd = $( "#eEndChkId:checked" ).val();
			//alert("Checkbox is " + chkd);
			if (chkd) {
				// set the end date to the start date
				// user can select a new date...
				var startDate = $( "#eStrtId" ).val();
				alert("Date Found " + startDate);
				$( "#eEndId" ).text(startDate);
				$( "#eEndId" ).datepicker();
				$( "#condEnd" ).fadeIn(2000);
			} else {
				$( "#condEnd" ).fadeOut(1000);
			}
		}
		
		
		$( ":checkbox").click(clickHandler);
		
		
		
		//$( "select#selRoomId" ).click(function() {
		//$( "select#selRoomId" ).select(function() {
		//function() {
		
		function updateSelectOptionList(){
			$.ajax({
				url: "/ws/getRoomData",
				//dataType: 'json',
				success: function(jsonData){
					//alert("Got this " + jsonData)
					//var options = '<option value="NONE"> Select </option>';
					var options = '';
					var jd = $.parseJSON(jsonData)
					for (var i = 0; i < jd.length; i++) {
						options += '<option value="' + jd[i].rid + '">' + jd[i].rname + ' (' + jd[i].rsize + ')' + '</option>';
					}
					//alert("here are options " + options)
					$("select#selRoomId").html(options);
				}
			});
			$.ajax({
				url: "/ws/getUserData",
				//dataType: 'json',
				success: function(jsonData){
					//alert("Got this " + jsonData)
					//var options = '<option value="NONE"> Select </option>';
					var options = '';
					var jd = $.parseJSON(jsonData)
					for (var i = 0; i < jd.length; i++) {
						options += '<option value="' + jd[i].uid + '">' + jd[i].namelast + ', ' + jd[i].namefirst + '</option>';
					}
					//alert("here are options " + options)
					$("select#selUserId").html(options);
				}
			});
			
		}
			
		updateSelectOptionList();
		
		/*
		 * Refresh the select field each time the tab link is clicked. 
		 */
		$( "#tabl-2" ).click(updateSelectOptionList) 
		$( "#tabl-3" ).click(updateSelectOptionList)
		
		/*
		 * Perform form validation
		 */
		//$("#createEvent").validate( function() {
			// Check for meeting conflicts
			

		//});
		
		$(":input").change(function(){
			/*
			 * Disable submit if input are changed after validataion
			 * 
			 */
			$("#eSubmit").attr('disabled', true);
		});
		
		$("input[id=eCheck]").click(function() {
			// Hide error messages
			$('#errMsgDiv').hide();
			// perform form validations
			if ($("#createEvent").validate().form()) {
				/*
				 * Only execute this code block 
				 * if the fields validation is successful
				 */
				$.extend({
					getJsonData: function() {
						var result = null;
						/*
						 * Note: Following attributes must set for the call back to work 	
						 * dataType: 'json',
						 * async: false
						 */ 
						$.ajax({
							url: "/ws/checkConflicts",
							dataType: 'json',
							async: false,
							data: $("#createEvent :input").serialize(),
							success: function(data) {
								result = data;
							}
						});
						// return result to caller
						return result;
					}
				})
				
				var result = $.getJsonData();
				
				var size = 0, key;
				for (key in result ) size ++;
				//alert("Result:" + size )
				/*-----------------------------------
				 * 
				 * Display Scheduling Errors
				 * 
				 * ----------------------------------
				 */
				if (size == 0 ) {
					// no conflicts, enable the create button
					$("#eSubmit").attr('disabled', false);
					$("#updateEventBtn").attr('disabled', false);
				} else {
					// build an error message log
					var errorMsg = "<div id='errorMsg'><div id='errorHdr'>Error: Schedule Conflicts Found.</div>";
					var errorAlt = "Error: Schedule Conflicts Found.\n";
					// detail the conflicts
					/*
					 * Note: This section is currently design execute discrete 
					 * username lookups. Additional (later) steps can be taken to 
					 * alter this code to perform batch look-ups...
					 */
					
					// collect errors with member attendance
					//alert("Attendee Errors");
					
					$.extend({
						getFullName: function(uid) {
							var result = null;
							/*
							 * Note: Following attributes must set for the call back to work 	
							 * dataType: 'json',
							 * async: false
							 */ 
							
							$.ajax({
								url: "/ws/getUserNameById",
								//dataType: 'json',
								async: false,
								data: {'uid': uid, 'getFullName': 1 },
								success: function(data) {
									result = data;
								}
							});
							// return result to caller
							return result;
						}
					})
					$.each(result, function(attr, val){
						// review each error update messate
						if (attr == 'Attendee') {
							
							errorMsg += "<div id='attendErr'> "; 
							errorMsg += "<div id='attendErrHdr'>The following attendees are not available: </div> ";
							errorAlt += "The following attendees are not available: \n"; 
							for (user in val) {
								var result = $.getFullName(user);
								errorMsg += "<div> ";
								errorMsg +=  result;
								errorAlt += "* " + result + "\n";
								errorMsg += "</div>";
							}
							errorMsg += "</div>";
							//alert("att: " + errorMsg);
						} else if (attr == 'isHosting') {
							errorMsg += "<div id='attendErr'> "; 
							errorMsg += "<div id='attendErrHdr'>The following attendees are not available: </div> ";
							errorAlt += "The following attendees Hosting Events: \n"; 
							for (user in val) {
								var result = $.getFullName(user);
								errorMsg += "<div> ";
								errorMsg +=  result;
								errorAlt += "* " + result + "\n";
								errorMsg += "</div>";
							}
							errorMsg += "</div>";
							//alert("att: " + errorMsg);
							
						} else {
							//Process error handles
							//alert("Error with : " + attr);
							if (attr == 'Host') {
								errorMsg += "<div id='hostErr'><div>You are not available</div></div>  "; 
								errorAlt += "- You are not available\n";
								
							} else if (attr == 'Room') {
								errorMsg += "<div id='roomErr'><div>The requested room is not available </div></div>";
								errorAlt += "- The requested room is not available\n"; 
								
							} else if (attr == 'RoomSize') {
								errorMsg += "<div id='sizeEr'><div>Room size is too small </div></div>"; 
								errorAlt += "- Room size is too small\n";
								
							} 
						}
					})
					errorMsg += "</div>";
					$('#errMsgDiv').html(errorMsg);
					alert(errorAlt);
					//$('#errMsgDiv').fadeIn(1000);
				}
			} 
		});
	});
	
	
</script>
